//
//  PHJSearchBar.swift
//  新浪微博
//
//  Created by jie peng on 16/5/20.
//  Copyright © 2016年 jie peng. All rights reserved.
//

import UIKit

class PHJSearchBar: UIView {

    @IBOutlet weak var rightCons: NSLayoutConstraint!
    @IBOutlet weak var cancelBtn: UIButton!
    @IBOutlet weak var searchBarTF: UITextField!
 
    
   
    
    @IBAction func cancelBtn(sender: UIButton) {
        
        self.cancelBtn.hidden = true
        UIView.animateWithDuration(0.25) { 
            
            self.rightCons.constant = 0
            self.layoutIfNeeded()
        }
        self.endEditing(true)
    }
    class func loadSearchBar () ->PHJSearchBar{
        let searchView = NSBundle.mainBundle().loadNibNamed("SearchBar", owner: nil, options: nil).last
        return searchView as! PHJSearchBar
        
    }
    //MARK:在awakeFromNib中初始化设置
    @IBAction func textFieldDidEdit(sender: UITextField) {
        self.cancelBtn.hidden = false
        self.rightCons.constant = self.cancelBtn.bounds.width
        
        layoutIfNeeded()
    }
    override func awakeFromNib() {
        self.cancelBtn.hidden = true

                self.bounds.size.width = ScreenWidth
        
        self.searchBarTF.leftView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        searchBarTF.leftViewMode = .Always
        searchBarTF.leftView?.frame = CGRect(x: 0, y: 0, width: 50, height: 40)
        searchBarTF.leftView?.contentMode = .Center
        
//        searchBarTF.layer.cornerRadius = 20
//        searchBarTF.layer.borderColor = UIColor.orangeColor().CGColor
//        
//        searchBarTF.layer.borderWidth = 2
//        searchBarTF.layer.masksToBounds = true
    }
}
